{{/*
    高级日志打印工具
    -----------------------------------
    参数:
    - level: 日志级别 (默认 "INFO")
    - prefix: 模块前缀 (可选)
    - message: 日志内容 (必须)
    示例:
    {{ partial "helpers/log" (dict "level" "ERROR" "prefix" "image" "message" "文件未找到") }}
    {{ partial "helpers/log" (dict "message" (printf "是否为生产环境: %v" hugo.IsProduction)) }}

    参考: https://gohugo.io/functions/fmt
*/}}

{{/* 默认值处理 */}}
{{ $level := upper (.level | default "INFO") }}
{{ $prefix := .prefix | default "" }}
{{ $timestamp := now.Format "2006-01-02 15:04:05" }}

{{/* 智能消息格式化 */}}
{{ $formatted := "" }}
{{ if reflect.IsMap .message }}
    {{ $formatted = printf "%#v" .message }}
{{ else if reflect.IsSlice .message }}
    {{ $formatted = delimit .message " | " }}
{{ else }}
    {{ $formatted = printf "%v" .message }}
{{ end }}

{{/* 构建日志前缀 */}}
{{ $logPrefix := printf "[%s] [%s]" $timestamp $level }}
{{ if $prefix }}
    {{ $logPrefix = printf "%s [%s]" $logPrefix $prefix }}
{{ end }}

{{/* 输出日志 */}}
{{ $fullMessage := printf "%s %s" $logPrefix $formatted }}
{{ warnf $fullMessage }}

{{/* 返回空字符串避免模板输出 */}}
{{ return "" }}